package com.nf.dao;

import com.nf.entity.Jdbc;
import com.nf.utils.DbHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JdbcDao {
    public List<Jdbc> selectAll(){
        List<Jdbc> jdbcs=new ArrayList<>();
        Connection connection= DbHelper.connection();
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        String sql="select * from jdbc";
        try {
            statement= connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                Jdbc jdbc=new Jdbc();
                jdbc.setId(resultSet.getInt("id"));
                jdbc.setName(resultSet.getString("name"));
                jdbcs.add(jdbc);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbHelper.colesAll(connection,statement,resultSet);
        }
        return jdbcs;
    }
    public void insert(Jdbc jdbc){
        Connection connection=DbHelper.connection();
        PreparedStatement statement=null;
        String sql="insert into jdbc values(?,?)";
        try {
            statement=connection.prepareStatement(sql);
            statement.setInt(1,jdbc.getId());
            statement.setString(2,jdbc.getName());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbHelper.colesConnAndStmt(connection,statement);
        }
    }
    public void delete(int id){
        Connection connection=DbHelper.connection();
        PreparedStatement statement=null;
        String sql="delete from jdbc where id=?";
        try {
            statement= connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbHelper.colesConnAndStmt(connection,statement);
        }
    }
    public Jdbc selectById(int id){
        Jdbc jdbc=new Jdbc();
        Connection connection=DbHelper.connection();
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        String sql="select * from jdbc where id=?";
        try {
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet=statement.executeQuery();
            if (resultSet.next()){
                jdbc.setId(resultSet.getInt("id"));
                jdbc.setName(resultSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbHelper.colesAll(connection,statement,resultSet);
        }
        return jdbc;
    }
    public void update(Jdbc jdbc){
        Connection connection=DbHelper.connection();
        PreparedStatement statement=null;
        String sql="update jdbc set name=? where id=?";
        try {
            statement= connection.prepareStatement(sql);
            statement.setString(1,jdbc.getName());
            statement.setInt(2,jdbc.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbHelper.colesConnAndStmt(connection,statement);
        }
    }
}
